$(document).ready(function(){
	/**
	 * flag:为true时表示输入正确，为false时表示提示信息
	 */
	var prompt = function(text, message,flag) {
		if(flag) {
			$(text).nextAll("span:last").html("<b class='aqxx_bg duihao_ico mr_2'></b>");
		}else {
			$(text).nextAll("span:last").html(message).removeClass("red").addClass("gray");
		}
	};
	var promptFailure = function(text, message) {
		$(text).nextAll("span:last").html("<b class='aqxx_bg gantan_ico mr_2'></b>"+message).removeClass("gray").addClass("red");
	};
	var goStep = function(step){
		$("#password_buzhou>span[class^='password_ico arrow_']").each(function(index){
			index ++;
			if(index == step)
			{
				$(this).attr("class", "password_ico arrow_yellow mr_10");
			}
			else
			{
				$(this).attr("class", "password_ico arrow_gray mr_10");
			}
		});
		$("#password_buzhou>span[class^='password_ico buzhou_']").each(function(index){
			index ++;
			if(index == step)
			{
				$(this).attr("class", "password_ico buzhou_"+index+"_yellow");
			}
			else
			{
				$(this).attr("class", "password_ico buzhou_"+index+"_gray");
			}
		});
		
	};
	//校验密码级别
	var checkPassLevel = function (string) {
	    return string.replace(/^(?:(?=.{4})(?=.*([a-z])|.)(?=.*([A-Z])|.)(?=.*(\d)|.)(?=.*(\W)|.).*|.*)$/, "$1$2$3$4").length;
	};
	/**
	 * 找回密码首页初始化
	 */
	var initRetrieveIndex = function() {
		/**
		 * 账号检查
		 */
		var checkUserName = function() {
			var text = $("#IuserName");
			var value = text.val();
			var flag = false;
			var errorMsg = "";
			var isEmail = /@/g.test(value);
			if(isEmail) {//邮箱方式
				errorMsg = Helper.validata.validataEmail(value);
				if(errorMsg == undefined){
					errorMsg = "";
					flag = true;
				}
			}else {
				if(Helper.isEmpty(value)){
					errorMsg = "您还没有输入帐号";
				}else {
					errorMsg = Helper.validata.validataUname(value);//验证用户名
					if(errorMsg == undefined) {//合法
						errorMsg = "";
						flag = true;
					}
				}
			}
			if(!flag)
			{
				promptFailure(text[0], errorMsg);
			}
			else
			{
				prompt(text[0], errorMsg);
			}
			return flag;
		};
		/**
		 * 用户名
		 */
		$("#IuserName").blur(checkUserName).focus(function(){
			prompt(this, "");
		});
		$("#confirm").click(function(){
			if(checkUserName()){
				/**
				 * 提交
				 */
				new Helper.RemoteTrans({
					url:Helper.basePath + "/goRetrieveType.htm",
					params:{"userNameOrEmail":$("#IuserName").val(),"pwdType":$("[name='IpwdType']:checked").val()},
					onSuccess: function(result){
						initRetrieveWay();
					},
					onError: function(result, status){
						Helper.message(result.message);
					}
				}).send();
			}
		});
		goStep(1);
	};
	
	/**
	 * 找回密码方式选择页初始化
	 */
	var initRetrieveWay = function() {
		new Helper.RemoteQuery({
			url:Helper.basePath + "/page/retrieve/sub/sub_password_way.jsp",
			renderId: "loaddiv",
			onSuccess:function(){
				goStep(2);
				if($("#byquestion_a").length > 0)
				{
					$("#byquestion_a").click(initByAnswerType);
				}
				if($("#bymobile_a").length > 0)
				{
					$("#bymobile_a").click(initByMobileType);
				}
				if($("#byemail_a").length > 0)
				{
					$("#byemail_a").click(initByEmailType);
				}
			}
		}).send();
	};
	
	/**
	 * 密保答案页面初始化
	 */
	var initByAnswerType = function() {
		new Helper.RemoteQuery({
			url:Helper.basePath + "/page/retrieve/sub/sub_password_question.jsp",
			renderId: "loaddiv",
			onSuccess:function(){
				/**
				 * 问题检查
				 */
				var checkQuestion = function() {
					var value = $("#Iquestion").text();
					if(Helper.isEmpty(value)) {
						return false;
					}else {
						return true;
					}
				};
				/**
				 * 答案检查
				 */
				var checkAnswer = function() {
					var text = $("#Ianswer");
					var value = text.val();
					var flag = false;
					var errorMsg = "";
					if(Helper.isEmpty(value)){
						errorMsg = "请输入密保答案";
					}else{
						flag = true;
					}
					if(!flag)
					{
						promptFailure(text[0], errorMsg);
					}
					else
					{
						prompt(text[0], errorMsg);
					}
					return flag;
				};
				
				$("#Ianswer").blur(checkAnswer).focus(function(){
					prompt(this, "");
				});;
				
				/**
				 * 密保答案校验
				 */
				var confirmAnswer = new Helper.RemoteTrans({
					url: Helper.basePath + "/doRetrievePwdByAnswer.htm",
					onSuccess: function(result){
						initRetrieveLast(result.returnValue);
					},
					onError: function(result, status){
						if(result.code === '9999') {
							Helper.message("非法操作", function(){location.href = Helper.basePath + "/goRetrievePwdIndex.htm";});
						}else {
							Helper.message(result.message);
						}
					}
				});
				$("#answer_confirm").click(function(){
					if(checkQuestion()){
						if(checkAnswer()){
							confirmAnswer.addParameters("answer", $("#Ianswer").val());
							confirmAnswer.send();
						}
					}else{
						Helper.message("非法操作", function(){location.href = Helper.basePath + "/goRetrievePwdIndex.htm";});
					}
				});
				$("#otherway_a").click(function(){
					initRetrieveWay();
				});
			}
		}).send();
	};
	/**
	 * 邮箱初始化
	 */
	var initByEmailType = function(){
		new Helper.RemoteQuery({
			url:Helper.basePath + "/page/retrieve/sub/sub_password_email.jsp",
			renderId: "loaddiv",
			onSuccess:function(){
				/**
				 * 邮箱验证
				 */
				var validEmail = function(){
					var text = $("#Iemail");
					var Iemail = text.val();
					var message = Helper.validata.validataEmail(Iemail);
					var flag = false;
					if(message != undefined) {
						promptFailure(text[0], message);
					} else {
						prompt(text[0], "");
						flag = true;
					}
					return flag;
				};
				
				/**
				 * 失去焦点时验证邮箱
				 */
				$("#Iemail").blur(validEmail).focus(function(){prompt(this, "");});
				
				/**
				 * 发送邮件
				 */
				var sendMail = new Helper.RemoteTrans({
					url: Helper.basePath + "/goRetrievePwdByEmail.htm",
					onSuccess: function(result){
						$("#email_confirm").unbind();
						$("#email_confirm").html("已发送");
						$("#email_confirm").attr("class", "free_access_gray black_a mr_10");
						//绑定登录事件
						var url = $("#Iemail").val().split("@")[1];
						if(/yahoo/.test(url)){
							url = "cn.yahoo.com/";
						}else if(/gmail/.test(url)){
							url = "google.com/";
						}else if(/163|126|yeah|qq|sina|sohu|hotmail|139|189|263/.test(url)){
						}else {
							url = undefined;
						}
						if(url)
						{
							$("#goMail").click(function(){
								window.open("http://mail."+url);
							});
							$("#goMail").show();
						}
						else
						{
							$("#goMail").remove();
						}
					},
					onError: function(result, status){
						if(result.code === '9999') {
							Helper.message("非法操作", function(){location.href = Helper.basePath + "/goRetrievePwdIndex.htm";});
						}else {
							Helper.message(result.message);
						}
					}
				});
				$("#email_confirm").click(function(){
					if(validEmail()){
						sendMail.addParameters("email", $("#Iemail").val());
						sendMail.send();
					}
				});
				$("#otherway_a").click(function(){
					initRetrieveWay();
				});
			}
		}).send();
	};
	/**
	 * 手机初始化
	 */
	var initByMobileType = function(){
		new Helper.RemoteQuery({
			url:Helper.basePath + "/page/retrieve/sub/sub_password_mobile.jsp",
			renderId: "loaddiv",
			onSuccess:function(){
				/**
				 * 手机校验
				 */
				var checkMobile = function(){
					var text = $("#Imobile");
					var value = text.val();
					var flag = false;
					var errorMsg = "";
					if(!Helper.isEmpty(value)){
						if(!Helper.validata.isMobile(value)){
							errorMsg = "请输入正确的手机号码";
						}
						else
						{
							flag = true;
						}
					}else {
						errorMsg = "请输入手机号码";
					}
					if(!flag)
					{
						promptFailure(text[0], errorMsg);
					}
					else
					{
						prompt(text[0], errorMsg);
					}
					return flag;
				};
				/**
				 * 手机验证码校验
				 */
				var checkValid = function(){
					var text = $("#Ivalid");
					var value = $("#Ivalid").val();
					var flag = false;
					var errorMsg = "";
					if(Helper.isEmpty(value)){
						errorMsg = "请输入验证码";
					}else {
						if(Helper.validata.getByteLength(value) === 4) {
							flag = true;
						}else {
							errorMsg = "验证码长度为4";
						}
					}
					if(!flag)
					{
						promptFailure(text[0], errorMsg);
					}
					else
					{
						prompt(text[0], errorMsg);
					}
					return flag;
				};
				
				$("#Imobile").blur(checkMobile).focus(function(){prompt(this, "");});
				$("#Ivalid").blur(checkValid).focus(function(){prompt(this, "");});
				Helper.doms.muberKeyupEmpty($("#Ivalid")[0]);
				/**
				 * 倒计时
				 */
				var timeInterval = 60;
				var intervalFun = null;
				var countDown = function(){
					if(timeInterval-- > 0) {
						$("#mobile_confirm").unbind();
						$("#mobile_confirm").next("span").text("已发送（"+timeInterval+"S）");
					}else{
						clearInterval(intervalFun);
						intervalFun = null;
						$("#mobile_confirm").next("span").text("");
						$("#mobile_confirm").attr("class", "free_access white_a mr_10");
						$("#mobile_confirm").one("click", function(){
							if(checkMobile()){
								mobileRemote.addParameters("mobile", $("#Imobile").val());
								mobileRemote.send();
							}
						});
					}
				};
				/**
				 * 发送手机验证码
				 */
				var mobileRemote = new Helper.RemoteTrans({
					url: Helper.basePath + "/goRetrievePwdByMobile.htm",
					onSuccess: function(result){
						$("#mobile_confirm").unbind();
						$("#mobile_confirm").attr("class", "free_access_gray black_a mr_10");
						$("#mobile_confirm").next("span").text("已发送");
						//显示短信倒计时提示
						timeInterval = result.returnValue;
						intervalFun = window.setInterval(countDown, 1000);
					},
					onError: function(result, status){
						if(result.code === '9999') {
							Helper.message("非法操作", function(){location.href = Helper.basePath + "/goRetrievePwdIndex.htm";});
						}else {
							Helper.message(result.message);
						}
					}
				});
				$("#mobile_confirm").click(function(){
					if(checkMobile()){
						mobileRemote.addParameters("mobile", $("#Imobile").val());
						mobileRemote.send();
					}
				});
				/**
				 * 短信验证
				 */
				var validByMobile = new Helper.RemoteTrans({
					url: Helper.basePath + "/doRetrievePwdByMobile.htm",
					onSuccess: function(result){
						initRetrieveLast(result.returnValue);
					},
					onError: function(result, status){
						if(result.code === '9999') {
							Helper.message("非法操作", function(){location.href = Helper.basePath + "/goRetrievePwdIndex.htm";});
						}else {
							Helper.message(result.message);
						}
					}
				});
				$("#smsValid_confirm").click(function(){
					if(checkValid()){
						validByMobile.addParameters("valid", $("#Ivalid").val());
						validByMobile.send();
					}
				});
				$("#otherway_a").click(function(){
					initRetrieveWay();
				});
			}
		}).send();
	};
	
	/**
	 * 找回密码重置密码初始化
	 */
	var initRetrieveLast = function(type) {
		new Helper.RemoteQuery({
			url:Helper.basePath + "/page/retrieve/sub/sub_password_reset.jsp",
			renderId: "loaddiv",
			params :{type:type},
			onSuccess:function(){
				goStep(3);
				var IbetCardNO = $("#IbetCardNO").val();
				var IpwdType = $("#IpwdType").val();
				var IfindType = $("#IfindType").val();
				/**
				 * 新密码校验
				 */
				var checkPassword = function() {
					var text = $("#InewPassword");
					var value = text.val();
					var flag = false;
					var message = Helper.validata.validataPass(value);
					if(undefined === message){
						flag = true;
						prompt(text[0], "", true);
					}else {
						promptFailure(text[0], message);
					}
					return flag;
				};
				/**
				 * 重复密码校验
				 */
				var checkRePassword = function() {
					var text = $("#IreNewPassword");
					var reValue = text.val();
					var flag = false;
					var value = $("#InewPassword").val();
					var message = Helper.validata.validataPass(reValue);
					if(undefined === message){
						if(value != reValue){
							promptFailure(text[0], "两次输入密码不一致,请仔细检查");
						}else {
							flag = true;
							prompt(text[0], "", true);
						}
					}else {
						promptFailure(text[0], "确认"+message);
					}
					return flag;
				};
				$("#InewPassword").blur(checkPassword).focus(function(){
					prompt(this, "");
				}).keyup(function(){
					var passLevel = checkPassLevel($(this).val());
					$("#passLevel>span").each(function(index){
						if(passLevel<2 && index == 0)
						{
							$(this).attr("class", "jibie_yellow_bg");
						}
						else if(passLevel == 2 && index == 1)
						{
							$(this).attr("class", "jibie_yellow_bg");
						}
						else if(passLevel > 2 && index == 2)
						{
							$(this).attr("class", "jibie_yellow_bg");
						}
						else
						{
							$(this).attr("class", "jibie_gray_bg");
						}
					});
				});
				$("#IreNewPassword").blur(checkRePassword).focus(function(){
					prompt(this, "");
				});
				
				var checkOther = function() {
					if(Helper.isEmpty(IbetCardNO) || Helper.isEmpty(IpwdType) || Helper.isEmpty(IfindType)) {
						return false;
					}else {
						return true;
					}
				};
				/**
				 * 修改密码
				 */
				var updatePwd = new Helper.RemoteTrans({
					url: Helper.basePath + "/doResetPwdForRetrievePwd.htm",
					onSuccess: function(result){
						new Helper.RemoteQuery({
							url:Helper.basePath + "/page/retrieve/sub/sub_password_success.jsp",
							params:{pwdType:IpwdType},
							renderId: "loaddiv",
							onSuccess:function(){
								goStep(4);
							}
						}).send();
					},
					onError: function(result, status){
						Helper.message(result.message);
					}
				});
				$("#updatePwd_confirm").click(function(){
					if(checkOther()){
						if(checkPassword() && checkRePassword()){
							updatePwd.addParameters("newPassword", $("#InewPassword").val());
							if("email" == IfindType || "mobile" == IfindType){
								updatePwd.addParameters("codeOrMessage", $("#Icode").val());
							}else if("question" == IfindType){
								updatePwd.addParameters("codeOrMessage", $("#Ianswer").val());
							}
							updatePwd.addParameters("betCardNO", IbetCardNO);
							updatePwd.addParameters("type", IpwdType);
							updatePwd.addParameters("findType", IfindType);
							updatePwd.send();
						}
					}else{
						Helper.message("非法操作", function(){location.href = Helper.basePath + "/goRetrievePwdIndex.htm";});
					}
				});
				$("#updatePwd_reset").click(function(){
					$("form")[0].reset();
				});
			}
		}).send();
	};
	/**
	 * 加载用户名输入页面
	 */
	var loadUserInput = new Helper.RemoteQuery({
		url:Helper.basePath + "/page/retrieve/sub/sub_password.jsp",
		renderId: "loaddiv",
		onSuccess:function(){
			initRetrieveIndex();
		}
	});
	if(!Helper.isEmpty($("#isValid").val()))
	{//如果是验证,则跳转到验证步骤
		if(eval($("#isValid").val()) && !Helper.isEmpty($("#validType").val()) && $("#validType").val() === 'email')
		{
			initRetrieveLast($("#validType").val());
		}
		else
		{
			Helper.message($("#validMessage").val());
			loadUserInput.send();
		}
	}
	else if(!Helper.isEmpty($("#userName").val()) && !Helper.isEmpty($("#pwdType").val()))
	{//如果有用户名和密码方式,则跳转到找回方式步骤
		new Helper.RemoteTrans({
			url:Helper.basePath + "/goRetrieveType.htm",
			params:{"userNameOrEmail":$("#userName").val(),"pwdType":$("#pwdType").val()},
			onSuccess: function(result){
				initRetrieveWay();
			},
			onError: function(result, status){
				loadUserInput.send();
			}
		}).send();
	}
	else
	{
		loadUserInput.send();
	}
});